#!/bin/bash
#pMusic
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8

case $1 in
	-write_config|-version_upgrade)
		false
		;;
	*) #these are for the gui only
		export TEXTDOMAIN=pmusic
		. $APPDIR/func -reset_gtk
		export THEMES=`ls "$APPDIR"/themes | awk '{print "<item>"$1"</item>"}'` #make theme list
		export LANGUAGES=`find /usr/share/locale -name 'pmusic.mo' | cut -d'/' -f5 | awk '{print "<item>"$1"</item>"}' | sort`
		export PMUSIC_FRONTENDS=`ls -1 "$APPDIR"/frontends | awk '{print "<item>"$1"</item>"}' | grep -vF '.pre</item>'`
		export CD_DEVICES=$(ls -1 /sys/block 2>> $STDERR | grep -E '^scd|^sr' | awk '{print "<item>/dev/"$1"</item>"}')
		export SOUND_DEVICES="<item>auto</item>`LC_ALL=C aplay -l | grep -F 'card ' | awk '{print "<item>"$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10"</item>"}'`"
		#To find available speakers, we must first detect soundcard number
		if [ "$SOUND_DEVICE" = "auto" ]; then
			if [ -f /etc/asound.conf ]; then #see if MultipleSoundCardWizard was used
				read TMP < /etc/asound.conf
				CARD_NR=${TMP##* } 
			fi
			if [ ! "$CARD_NR" ]; then
				TMP="`aplay -l | grep -m1 -F card`" #grab the first card
				CARD_NR=${TMP%%:*}; CARD_NR=${CARD_NR#* }
			fi
		else
			CARD_NR=${SOUND_DEVICE%%:*}; CARD_NR=${CARD_NR#* }
		fi
		export SPEAKERS=`LC_ALL=C amixer -c $CARD_NR controls | grep -F 'Playback Volume' | cut -d "'" -f 2 | sed "s/ Playback Volume//g" | tr -s ' ' '_' | awk '{print "<item>"$1"</item>"}' | tr -s '_' ' '`

		#these TEXT variable is in use both as tooltips and in the help box
		TEXT0="`gettext 'Depending of how you set up pMusic, it may require some disk space. Please point Data storage to a suitable location.'`"
		TEXT1="`gettext 'Master volume most often is a mono channel, and does not support different level for left and right speaker. You must specify correct channel for your system to get mixer volume to work as a balance controller.'`"
		TEXT2="`gettext 'Auto is the recommended choice , but there are 3
reasons for setting your soundcard:
1. If there is installed more than 1 card you might
want to define which one to use.
2. If you struggles with bad sound quality, defining
your card could improve playback.
3. If you NOT want to send audio signal through the equalizer (Bypass).

! You must restart preferences window to rebuild speaker list
for your chosen soundcard.

! By setting the souncard you also bound the alsa driver to
pMusic. This means other programs will not play audio as long
as pMusic is active. On the other hand, pMusic will not
play if sound driver is in use by other program.

! The equalizer requires soundcard set to Auto.'`"

		#build tab playback. This is used alone in the .CD preset
		. $APPDIR/gui_mixer #normalize
		S='
		<vbox margin="10">
		 <text height-request="5"><label>""</label></text>
		 <hbox tooltip-text="'$TEXT1'">
		  <text><label>'$(gettext 'Speaker channel')'</label></text>
		  <comboboxtext width-request="150">
		   <variable>SPEAKER</variable>
		   <item>'$SPEAKER'</item>
		   '$SPEAKERS'
		  </comboboxtext>
		 </hbox>
		 <hbox tooltip-text="'$TEXT2'">
		  <text><label>'$(gettext 'Sound card')'</label></text>
		  <comboboxtext width-request="150">
		   <variable>SOUND_DEVICE</variable>
		   <item>'$SOUND_DEVICE'</item>
		   '$SOUND_DEVICES'
		  </comboboxtext>
		 </hbox>
		 <hbox>
		  <text><label>'$(gettext 'CD device')'</label></text>
		  <comboboxtext width-request="150">
		   <variable>CD_DEVICE</variable>
		   <item>'$CD_DEVICE'</item>
		   '$CD_DEVICES'
		  </comboboxtext>
		 </hbox>
		 <vbox '$ST'>
		  <text><label>""</label></text>
		 </vbox>
		 <vbox '$SF'>
		 <checkbox>
		  <label>'$(gettext 'Give pMusic higher CPU-priority (to avoid stutter)')'</label>
		  <variable>HIGH_CPU</variable>
		  <default>'$HIGH_CPU'</default>
		 </checkbox>
		 '$GUI_NORMALIZE'
		 <text height-request="5"><label>""</label></text>
		 <hbox>
		  <hbox '$SF'>
		   <entry width-request="30">
		    <variable>GAP</variable>
		    <default>"'$GAP'"</default>
		   </entry>
		  </hbox>
		  <text '$SF'><label>'$(gettext 'Gap between tracks (in seconds)')'</label></text>
		  <text '$ST'><label>""</label></text>
		 </hbox>
		 </vbox>
		</vbox>'
		TAB_PLAYBACK="$S"

		TAB_CDDB='
		<vbox margin="10">
		 <checkbox>
		  <label>'$(gettext 'Grab online Artist/Title info if exist')'</label>
		  <variable>CDDB</variable>
		  <default>'$CDDB'</default>
		 </checkbox>
		 <text height-request="1"><label>""</label></text>
		 <text xalign="0"><label>'$(gettext 'If your CD is not registrated in the worldwide cddb database, it would probably use info for another random album. If so, please deactivate this option.')'</label></text>
		</vbox>'
		;;
esac

write_config (){
	OLD_CONFIG="$(<$CONFIG)"
	#running pMusic with the -j switch should NOT save PMUSIC_FRONTEND/geometry/search settings
	[ -s $WORKDIR/DONT_SAVE_FRONTEND ] && read DONT_SAVE_FRONTEND < $WORKDIR/DONT_SAVE_FRONTEND
	[ -s $WORKDIR/window_title ] && XWININFO="`LC_ALL=C xwininfo -stats -name "$(<$WORKDIR/window_title)" 2>> $STDERR`"
	if [ "$FIRST_RUN" = "true" ]; then
		false
	elif [ "$DONT_SAVE_FRONTEND" = "true" ] || [ ! "$XWININFO" ]; then #keep original values
		read PMUSIC_FRONTEND C <<< ${OLD_CONFIG#*PMUSIC_FRONTEND=}
		read THEME C <<< ${OLD_CONFIG#*THEME=}; THEME=${THEME//\"/}
		read SEARCH_MYMUSIC C <<< ${OLD_CONFIG#*SEARCH_MYMUSIC=}
		read SEARCH_COLLECTIONS C <<< ${OLD_CONFIG#*SEARCH_COLLECTIONS=}
		read SEARCH_ALBUMART C <<< ${OLD_CONFIG#*SEARCH_ALBUMART=}
		read SEARCH_LYRICS C <<< ${OLD_CONFIG#*SEARCH_LYRICS=}
		read SEARCH_PODCAST C <<< ${OLD_CONFIG#*SEARCH_PODCAST=}
		read SEARCH_YOUTUBE C <<< ${OLD_CONFIG#*SEARCH_YOUTUBE=}
		read SEARCH_JAMENDO C <<< ${OLD_CONFIG#*SEARCH_JAMENDO=}
		read SEARCH_RADIO C <<< ${OLD_CONFIG#*SEARCH_RADIO=}
		read SEARCH_FILES C <<< ${OLD_CONFIG#*SEARCH_FILES=}
		read WINDOW_HEIGHT C <<< ${OLD_CONFIG#*WINDOW_HEIGHT=}; WINDOW_HEIGHT=${WINDOW_HEIGHT//\"/}
		read WINDOW_WIDTH C <<< ${OLD_CONFIG#*WINDOW_WIDTH=}; WINDOW_WIDTH=${WINDOW_WIDTH//\"/}
		read WINDOW_X C <<< ${OLD_CONFIG#*WINDOW_X=}; WINDOW_X=${WINDOW_X//\"/}
		read WINDOW_Y C <<< ${OLD_CONFIG#*WINDOW_Y=}; WINDOW_Y=${WINDOW_Y//\"/}
	else #save geometry
		GEOMETRY=${XWININFO#*-geometry }
		GEOMETRY=${GEOMETRY%%\+*}
		WINDOW_WIDTH=${GEOMETRY%x*}
		WINDOW_HEIGHT=${GEOMETRY#*x}
		read X1 C <<< ${XWININFO#*Absolute upper-left X:  }
		read Y1 C <<< ${XWININFO#*Absolute upper-left Y:  }
		read X2 C <<< ${XWININFO#*Relative upper-left X:  }
		read Y2 C <<< ${XWININFO#*Relative upper-left Y:  }
		WINDOW_X=$(($X1-$X2))
		WINDOW_Y=$(($Y1-$Y2))
	fi

	#write values to rc. If not existing, use values from original rc. eg if using limited preferences in .Radio
	echo '#pMusic config' > $CONFIG
	[ ! "$VERSION" ] && read VERSION C <<< ${OLD_CONFIG#*VERSION=}
	echo "export VERSION=$VERSION" >> $CONFIG
	echo -e '\n#Values found in the main preferences window' >> $CONFIG
	[ ! "$STORAGE_DIR" ] && read STORAGE_DIR C <<< ${OLD_CONFIG#*STORAGE_DIR=}; STORAGE_DIR=${STORAGE_DIR//\"/}
	echo "export STORAGE_DIR=\"$STORAGE_DIR\"" >> $CONFIG
	[ ! "$BROWSERDIR" ] && read BROWSERDIR C <<< ${OLD_CONFIG#*BROWSERDIR=}; BROWSERDIR=${BROWSERDIR//\"/}
	echo "export BROWSERDIR=\"$BROWSERDIR\"" >> $CONFIG
	[ ! "$TOOLTIPS" ] && read TOOLTIPS C <<< ${OLD_CONFIG#*TOOLTIPS=}
	echo "export TOOLTIPS=$TOOLTIPS" >> $CONFIG
	[ ! "$THEME" ] && read THEME C <<< ${OLD_CONFIG#*THEME=}; THEME=${THEME//\"/}
	echo "export THEME=\"$THEME\"" >> $CONFIG
	[ ! "$MY_LANGUAGE" ] && read MY_LANGUAGE C <<< ${OLD_CONFIG#*MY_LANGUAGE=}
	echo "export MY_LANGUAGE=$MY_LANGUAGE" >> $CONFIG
	[ ! "$PMUSIC_FRONTEND" ] && read PMUSIC_FRONTEND C <<< ${OLD_CONFIG#*PMUSIC_FRONTEND=}
	echo "export PMUSIC_FRONTEND=$PMUSIC_FRONTEND" >> $CONFIG
	[ ! "$AUDIO_EDITOR" ] && read AUDIO_EDITOR C <<< ${OLD_CONFIG#*AUDIO_EDITOR=}
	echo "export AUDIO_EDITOR=$AUDIO_EDITOR" >> $CONFIG
	[ ! "$BROWSER" ] && read BROWSER C <<< ${OLD_CONFIG#*BROWSER=}
	echo "export BROWSER=$BROWSER" >> $CONFIG
	[ ! "$TEXT_EDITOR" ] && read TEXT_EDITOR C <<< ${OLD_CONFIG#*TEXT_EDITOR=}
	echo "export TEXT_EDITOR=$TEXT_EDITOR" >> $CONFIG
	[ ! "$IMAGE_EDITOR" ] && read IMAGE_EDITOR C <<< ${OLD_CONFIG#*IMAGE_EDITOR=}
	echo "export IMAGE_EDITOR=$IMAGE_EDITOR" >> $CONFIG
	[ ! "$SOUND_DEVICE" ] && read SOUND_DEVICE C <<< ${OLD_CONFIG#*SOUND_DEVICE=}; SOUND_DEVICE=${SOUND_DEVICE//\"/}
	echo "export SOUND_DEVICE=\"$SOUND_DEVICE\"" >> $CONFIG
	[ ! "$SPEAKER" ] && read SPEAKER C <<< ${OLD_CONFIG#*SPEAKER=}; SPEAKER=${SPEAKER//\"/}
	echo "export SPEAKER=\"$SPEAKER\"" >> $CONFIG
	[ ! "$HIGH_CPU" ] && read HIGH_CPU C <<< ${OLD_CONFIG#*HIGH_CPU=}
	echo "export HIGH_CPU=$HIGH_CPU" >> $CONFIG
	[ ! "$NORMALIZE" ] && read NORMALIZE C <<< ${OLD_CONFIG#*NORMALIZE=}
	echo "export NORMALIZE=$NORMALIZE" >> $CONFIG
	[ ! "$GAP" ] && read GAP C <<< ${OLD_CONFIG#*GAP=}
	echo "export GAP=$GAP" >> $CONFIG
	[ ! "$CD_DEVICE" ] && read CD_DEVICE C <<< ${OLD_CONFIG#*CD_DEVICE=}
	echo "export CD_DEVICE=$CD_DEVICE" >> $CONFIG
	[ ! "$VIEW_SEARCH_OPTIONS" ] && read VIEW_SEARCH_OPTIONS C <<< ${OLD_CONFIG#*VIEW_SEARCH_OPTIONS=}
	echo "export VIEW_SEARCH_OPTIONS=$VIEW_SEARCH_OPTIONS" >> $CONFIG
	[ ! "$VIEW_ADD_OPTIONS" ] && read VIEW_ADD_OPTIONS C <<< ${OLD_CONFIG#*VIEW_ADD_OPTIONS=}
	echo "export VIEW_ADD_OPTIONS=$VIEW_ADD_OPTIONS" >> $CONFIG
	[ ! "$USE_META" ] && read USE_META C <<< ${OLD_CONFIG#*USE_META=}
	echo "export USE_META=$USE_META" >> $CONFIG
	[ ! "$LYRICS_LOCAL" ] && read LYRICS_LOCAL C <<< ${OLD_CONFIG#*LYRICS_LOCAL=}
	echo "export LYRICS_LOCAL=$LYRICS_LOCAL" >> $CONFIG
	[ ! "$ALBUMART_LOCAL" ] && read ALBUMART_LOCAL C <<< ${OLD_CONFIG#*ALBUMART_LOCAL=}
	echo "export ALBUMART_LOCAL=$ALBUMART_LOCAL" >> $CONFIG
	[ ! "$PMU_LOCAL" ] && read PMU_LOCAL C <<< ${OLD_CONFIG#*PMU_LOCAL=}
	echo "export PMU_LOCAL=$PMU_LOCAL" >> $CONFIG
	[ ! "$CDDB" ] && read CDDB C <<< ${OLD_CONFIG#*CDDB=}
	echo "export CDDB=$CDDB" >> $CONFIG
	echo '#misc' >> $CONFIG
	[ ! "$VIDEO_SERVER" ] && read VIDEO_SERVER C <<< ${OLD_CONFIG#*VIDEO_SERVER=}
	echo "export VIDEO_SERVER=$VIDEO_SERVER" >> $CONFIG
	[ ! "$RADIOHITS_DEL_ADDS" ] && read RADIOHITS_DEL_ADDS C <<< ${OLD_CONFIG#*RADIOHITS_DEL_ADDS=}
	echo "export RADIOHITS_DEL_ADDS=$RADIOHITS_DEL_ADDS" >> $CONFIG

	echo -e '\n#Values set elsewhere\n\n#search' >> $CONFIG

	[ ! "$SEARCH_MYMUSIC" ] && read SEARCH_MYMUSIC C <<< ${OLD_CONFIG#*SEARCH_MYMUSIC=}
	echo "export SEARCH_MYMUSIC=$SEARCH_MYMUSIC" >> $CONFIG
	[ ! "$SEARCH_COLLECTIONS" ] && read SEARCH_COLLECTIONS C <<< ${OLD_CONFIG#*SEARCH_COLLECTIONS=}
	echo "export SEARCH_COLLECTIONS=$SEARCH_COLLECTIONS" >> $CONFIG
	[ ! "$SEARCH_ALBUMART" ] && read SEARCH_ALBUMART C <<< ${OLD_CONFIG#*SEARCH_ALBUMART=}
	echo "export SEARCH_ALBUMART=$SEARCH_ALBUMART" >> $CONFIG
	[ ! "$SEARCH_LYRICS" ] && read SEARCH_LYRICS C <<< ${OLD_CONFIG#*SEARCH_ALBUMART=}
	echo "export SEARCH_LYRICS=$SEARCH_LYRICS" >> $CONFIG
	[ ! "$SEARCH_PODCAST" ] && read SEARCH_PODCAST C <<< ${OLD_CONFIG#*SEARCH_PODCAST=}
	echo "export SEARCH_PODCAST=$SEARCH_PODCAST" >> $CONFIG
	[ ! "$SEARCH_YOUTUBE" ] && read SEARCH_YOUTUBE C <<< ${OLD_CONFIG#*SEARCH_YOUTUBE=}
	echo "export SEARCH_YOUTUBE=$SEARCH_YOUTUBE" >> $CONFIG
	[ ! "$SEARCH_JAMENDO" ] && read SEARCH_JAMENDO C <<< ${OLD_CONFIG#*SEARCH_JAMENDO=}
	echo "export SEARCH_JAMENDO=$SEARCH_JAMENDO" >> $CONFIG
	[ ! "$SEARCH_RADIO" ] && read SEARCH_RADIO C <<< ${OLD_CONFIG#*SEARCH_RADIO=}
	echo "export SEARCH_RADIO=$SEARCH_RADIO" >> $CONFIG
	[ ! "$SEARCH_FILES" ] && read SEARCH_FILES C <<< ${OLD_CONFIG#*SEARCH_FILES=}
	echo "export SEARCH_FILES=$SEARCH_FILES" >> $CONFIG
	[ ! "$SEARCHPATH" ] && read SEARCHPATH C <<< ${OLD_CONFIG#*SEARCHPATH=}; SEARCHPATH=${SEARCHPATH//\"/}
	echo "export SEARCHPATH=\"$SEARCHPATH\"" >> $CONFIG
	echo '#Smartadd' >> $CONFIG
	[ ! "$SMARTADD_NR" ] && read SMARTADD_NR C <<< ${OLD_CONFIG#*SMARTADD_NR=}
	echo "export SMARTADD_NR=$SMARTADD_NR" >> $CONFIG
	[ ! "$SMARTADD_RANDOM" ] && read SMARTADD_RANDOM C <<< ${OLD_CONFIG#*SMARTADD_RANDOM=}
	echo "export SMARTADD_RANDOM=$SMARTADD_RANDOM" >> $CONFIG
	[ ! "$SMARTADD_SOURCE" ] && read SMARTADD_SOURCE C <<< ${OLD_CONFIG#*SMARTADD_SOURCE=}; SMARTADD_SOUCRE=${SMARTADD_SOURCE//\"/}
	echo "export SMARTADD_SOURCE=\"$SMARTADD_SOURCE\"" >> $CONFIG
	[ ! "$SMARTADD_RATE" ] && read SMARTADD_RATE C <<< ${OLD_CONFIG#*SMARTADD_RATE=}
	echo "export SMARTADD_RATE=$SMARTADD_RATE" >> $CONFIG
	[ ! "$SMARTADD_BPM_LO" ] && read SMARTADD_BPM_LO C <<< ${OLD_CONFIG#*SMARTADD_BPM_LO=}
	echo "export SMARTADD_BPM_LO=$SMARTADD_BPM_LO" >> $CONFIG
	[ ! "$SMARTADD_BPM_HI" ] && read SMARTADD_BPM_HI C <<< ${OLD_CONFIG#*SMARTADD_BPM_HI=}
	echo "export SMARTADD_BPM_HI=$SMARTADD_BPM_HI" >> $CONFIG
	[ ! "$SMARTADD_GENRE" ] && read SMARTADD_GENRE C <<< ${OLD_CONFIG#*SMARTADD_GENRE=}; SMARTADD_GENRE=${SMARTADD_GENRE//\"/}
	echo "export SMARTADD_GENRE=\"$SMARTADD_GENRE\"" >> $CONFIG
	echo '#Albumart size' >> $CONFIG
	[ ! "$ALBUMART_SEARCH_HEIGHT" ] && read ALBUMART_SEARCH_HEIGHT C <<< ${OLD_CONFIG#*ALBUMART_SEARCH_HEIGHT=}
	echo "export ALBUMART_SEARCH_HEIGHT=$ALBUMART_SEARCH_HEIGHT" >> $CONFIG

	echo -e '\n#The variables below are not set in any preferences gui' >> $CONFIG
	echo 'export FIRST_RUN=false' >> $CONFIG
	[ ! "$UPDATE_INTERVAL" ] && read UPDATE_INTERVAL C <<< ${OLD_CONFIG#*UPDATE_INTERVAL=}
	echo "export UPDATE_INTERVAL=$UPDATE_INTERVAL" >> $CONFIG
	[ ! "$RATING_PERIODE" ] && read RATING_PERIODE C <<< ${OLD_CONFIG#*RATING_PERIODE=}
	echo "export RATING_PERIODE=$RATING_PERIODE" >> $CONFIG
	[ ! "$SEARCH_WHILE_TYPING" ] && read SEARCH_WHILE_TYPING C <<< ${OLD_CONFIG#*SEARCH_WHILE_TYPING=}
	echo "export SEARCH_WHILE_TYPING=$SEARCH_WHILE_TYPING" >> $CONFIG
	echo -e '#Sourcelist columns' >> $CONFIG
	[ ! "$COLUMN_ARTIST" ] && read COLUMN_ARTIST C <<< ${OLD_CONFIG#*COLUMN_ARTIST=}
	echo "export COLUMN_ARTIST=$COLUMN_ARTIST" >> $CONFIG
	[ ! "$COLUMN_TITLE" ] && read COLUMN_TITLE C <<< ${OLD_CONFIG#*COLUMN_TITLE=}
	echo "export COLUMN_TITLE=$COLUMN_TITLE" >> $CONFIG
	[ ! "$COLUMN_ALBUM" ] && read COLUMN_ALBUM C <<< ${OLD_CONFIG#*COLUMN_ALBUM=}
	echo "export COLUMN_ALBUM=$COLUMN_ALBUM" >> $CONFIG
	[ ! "$COLUMN_NR" ] && read COLUMN_NR C <<< ${OLD_CONFIG#*COLUMN_NR=}
	echo "export COLUMN_NR=$COLUMN_NR" >> $CONFIG
	[ ! "$COLUMN_YEAR" ] && read COLUMN_YEAR C <<< ${OLD_CONFIG#*COLUMN_YEAR=}
	echo "export COLUMN_YEAR=$COLUMN_YEAR" >> $CONFIG
	[ ! "$COLUMN_GENRE" ] && read COLUMN_GENRE C <<< ${OLD_CONFIG#*COLUMN_GENRE=}
	echo "export COLUMN_GENRE=$COLUMN_GENRE" >> $CONFIG
	[ ! "$COLUMN_COMMENT" ] && read COLUMN_COMMENT C <<< ${OLD_CONFIG#*COLUMN_COMMENT=}
	echo "export COLUMN_COMMENT=$COLUMN_COMMENT" >> $CONFIG
	[ ! "$COLUMN_RATING" ] && read COLUMN_RATING C <<< ${OLD_CONFIG#*COLUMN_RATING=}
	echo "export COLUMN_RATING=$COLUMN_RATING" >> $CONFIG
	[ ! "$COLUMN_FORMAT" ] && read COLUMN_FORMAT C <<< ${OLD_CONFIG#*COLUMN_FORMAT=}
	echo "export COLUMN_FORMAT=$COLUMN_FORMAT" >> $CONFIG
	[ ! "$COLUMN_BITRATE" ] && read COLUMN_BITRATE C <<< ${OLD_CONFIG#*COLUMN_BITRATE=}
	echo "export COLUMN_BITRATE=$COLUMN_BITRATE" >> $CONFIG
	[ ! "$COLUMN_LENGTH" ] && read COLUMN_LENGTH C <<< ${OLD_CONFIG#*COLUMN_LENGTH=}
	echo "export COLUMN_LENGTH=$COLUMN_LENGTH" >> $CONFIG
	[ ! "$COLUMN_TEMPO" ] && read COLUMN_TEMPO C <<< ${OLD_CONFIG#*COLUMN_TEMPO=}
	echo "export COLUMN_TEMPO=$COLUMN_TEMPO" >> $CONFIG
	[ ! "$COLUMN_PATH" ] && read COLUMN_PATH C <<< ${OLD_CONFIG#*COLUMN_PATH=}
	echo "export COLUMN_PATH=$COLUMN_PATH" >> $CONFIG

	echo -e '\n#Window geometry' >> $CONFIG
	[ ! "$WINDOW_HEIGHT" ] && read WINDOW_HEIGHT C <<< ${OLD_CONFIG#*WINDOW_HEIGHT=}; WINDOW_HEIGHT=${WINDOW_HEIGHT//\"/}
	echo "export WINDOW_HEIGHT=\"$WINDOW_HEIGHT\"" >> $CONFIG
	[ ! "$WINDOW_WIDTH" ] && read WINDOW_WIDTH C <<< ${OLD_CONFIG#*WINDOW_WIDTH=}; WINDOW_WIDTH=${WINDOW_WIDTH//\"/}
	echo "export WINDOW_WIDTH=\"$WINDOW_WIDTH\"" >> $CONFIG
	[ ! "$WINDOW_X" ] && read WINDOW_X C <<< ${OLD_CONFIG#*WINDOW_X=}; WINDOW_X=${WINDOW_X//\"/}
	echo "export WINDOW_X=\"$WINDOW_X\"" >> $CONFIG
	[ ! "$WINDOW_Y" ] && read WINDOW_Y C <<< ${OLD_CONFIG#*WINDOW_Y=}; WINDOW_Y=${WINDOW_Y//\"/}
	echo "export WINDOW_Y=\"$WINDOW_Y\"" >> $CONFIG
}

export -f write_config


case "$1" in
-cd)
	$APPDIR/func_svg -heading "$(gettext 'CD Preferences')" > /dev/null
	export pmusic_preferences='
	<window title="pMusic - '$(gettext 'CD Preferences')'" icon-name="pmusic">
	 <vbox>
	  <vbox '$SF'>
	   <hbox spacing="0" height="75" scrollable="true" '$ST' vscrollbar-policy="2" hscrollbar-policy="2">
	    <eventbox name="svgheading" '$ST'>
	     <hbox '$ST'>
	      <text '$ST'><label>""</label></text>
	      <hbox spacing="0" '$SF'>
	       <vbox '$SF'>
	        <button relief="2" can-focus="false" '$SF'>
	         <input file stock="gtk-dialog-warning"></input>
	         <action>'$APPDIR'/func -about -disclaimer > /dev/null &</action>
	        </button>
	        <text '$ST'><label>""</label></text>
	       </vbox>
	       <vbox>
	        <button relief="2" can-focus="false" '$SF'>
	         <input file stock="gtk-help"></input>
	         <action>'$0' -info > /dev/null</action>
	        </button>
	        <text '$ST'><label>""</label></text>
	       </vbox>
	      </hbox>
	     </hbox>
	    </eventbox>
	   </hbox>
	  </vbox>
  
	  <notebook tab-pos="0" labels="'$(gettext 'Playback')'|'$(gettext 'Trackinfo')'" '$ST'>
	   '$TAB_PLAYBACK'
	   '$TAB_CDDB'
	  </notebook>
	  <hbox '$SF'>
	   <button cancel></button>
	   <button ok></button>
	  </hbox>
	 </vbox>
	</window>'
	[ $TOOLTIPS = false ] && pmusic_preferences="`echo "$pmusic_preferences" | sed 's%tooltip-%tooltipXXX%g'`" #deactivate tooltips
	I=$IFS; IFS=""
	for STATEMENTS in $($GTKDIALOG -p pmusic_preferences); do
		eval $STATEMENTS
	done
	IFS=$I
	export CDDB=$CDDB
	[ "$EXIT" = "OK" ] && write_config
	;;
-radio)
	. $APPDIR/gui_misc #grab gtkdialog xml-code for radio-index install
	$APPDIR/func_svg -heading "$(gettext 'Radio Preferences')" > /dev/null
	export pmusic_preferences='
	<window title="pMusic - '$(gettext 'Radio Preferences')'" icon-name="pmusic">
	 <vbox>
	  <vbox '$SF'>
	   <hbox spacing="0" height="75" scrollable="true" '$ST' vscrollbar-policy="2" hscrollbar-policy="2">
	    <eventbox name="svgheading" '$ST'>
	     <hbox '$ST'>
	      <text '$ST'><label>""</label></text>
	      <hbox spacing="0" '$SF'>
	       <vbox '$SF'>
	        <button relief="2" can-focus="false" '$SF'>
	         <input file stock="gtk-dialog-warning"></input>
	         <action>'$APPDIR'/func -about -disclaimer > /dev/null &</action>
	        </button>
	        <text '$ST'><label>""</label></text>
	       </vbox>
	       <vbox>
	        <button relief="2" can-focus="false" '$SF'>
	         <input file stock="gtk-help"></input>
	         <action>'$0' -info > /dev/null</action>
	        </button>
	        <text '$ST'><label>""</label></text>
	       </vbox>
	      </hbox>
	     </hbox>
	    </eventbox>
	   </hbox>
	  </vbox>

	  <notebook tab-pos="0" labels="'$(gettext 'Radio')'|'$(gettext 'Playback')'" '$ST'>
	   <vbox>
	    <hbox margin="10">
	     <button>
	      <input file stock="gtk-go-down"></input>
	      <label>'$(gettext 'Setup Radio stations')'</label>
	      <action>'$APPDIR'/func_radio</action>
	     </button>
	    </hbox>
	   </vbox>
	   '$TAB_PLAYBACK'
	  </notebook>
	  <hbox '$SF'>
	   <button cancel></button>
	   <button ok></button>
	  </hbox>
	 </vbox>
	</window>'
	[ $TOOLTIPS = false ] && pmusic_preferences="`echo "$pmusic_preferences" | sed 's%tooltip-%tooltipXXX%g'`" #deactivate tooltips
	I=$IFS; IFS=""
	for STATEMENTS in $($GTKDIALOG -p pmusic_preferences); do
		eval $STATEMENTS
	done
	IFS=$I
	[ "$EXIT" = "OK" ] && write_config
	;;
-podcast)
	$APPDIR/func_svg -heading "$(gettext 'Radio Preferences')" > /dev/null
	export pmusic_preferences='
	<window title="pMusic - '$(gettext 'Podcast Preferences')'" icon-name="pmusic">
	 <vbox>
	  <vbox '$SF'>
	   <hbox spacing="0" height="75" scrollable="true" '$ST' vscrollbar-policy="2" hscrollbar-policy="2">
	    <eventbox name="svgheading" '$ST'>
	     <hbox '$ST'>
	      <text '$ST'><label>""</label></text>
	      <hbox spacing="0" '$SF'>
	       <vbox '$SF'>
	        <button relief="2" can-focus="false" '$SF'>
	         <input file stock="gtk-dialog-warning"></input>
	         <action>'$APPDIR'/func -about -disclaimer > /dev/null &</action>
	        </button>
	        <text '$ST'><label>""</label></text>
	       </vbox>
	       <vbox>
	        <button relief="2" can-focus="false" '$SF'>
	         <input file stock="gtk-help"></input>
	         <action>'$0' -info > /dev/null</action>
	        </button>
	        <text '$ST'><label>""</label></text>
	       </vbox>
	      </hbox>
	     </hbox>
	    </eventbox>
	   </hbox>
	  </vbox>

	  <notebook tab-pos="0" labels="'$(gettext 'Podcast')'|'$(gettext 'Playback')'" '$ST'>
	   <vbox>
	    <hbox margin="10">
	     <button>
	      <input file stock="gtk-go-down"></input>
	      <label>'$(gettext 'Setup Podcast channels')'</label>
	      <action>'$APPDIR'/func_podcast -setup</action>
	     </button>
	    </hbox>
	   </vbox>
	   '$TAB_PLAYBACK'
	  </notebook>
	  <hbox '$SF'>
	   <button cancel></button>
	   <button ok></button>
	  </hbox>
	 </vbox>
	</window>'
	[ $TOOLTIPS = false ] && pmusic_preferences="`echo "$pmusic_preferences" | sed 's%tooltip-%tooltipXXX%g'`" #deactivate tooltips
	I=$IFS; IFS=""
	for STATEMENTS in $($GTKDIALOG -p pmusic_preferences); do
		eval $STATEMENTS
	done
	IFS=$I
	[ "$EXIT" = "OK" ] && write_config
	;;
-playback)
	export pmusic_preferences='
	<window title="pMusic - '$(gettext 'Preferences')'" icon-name="pmusic">
	 <vbox>
	  '"$($APPDIR/func_svg -heading "$(gettext 'Preferences')")"'
	  '$TAB_PLAYBACK'
	  <hbox>
	   <button cancel></button>
	   <button ok></button>
	  </hbox>
	 </vbox>
	</window>'
	[ $TOOLTIPS = false ] && pmusic_preferences="`echo "$pmusic_preferences" | sed 's%tooltip-%tooltipXXX%g'`" #deactivate tooltips
	I=$IFS; IFS=""
	for STATEMENTS in $($GTKDIALOG -p pmusic_preferences); do
		eval $STATEMENTS
	done
	IFS=$I
	[ "$EXIT" = "OK" ] && write_config
	;;

-info)
	HEADING="$(gettext 'Preferences')"
	IMG='info'
	FRAME="$(gettext 'info')"
	TXT1="<b>$(gettext 'Data storage')</b>
$TEXT0

<b>$(gettext 'Playback speaker')</b>
$TEXT1

<b>$(gettext 'Playback sound card')</b>
$TEXT2"
	. $APPDIR/box_ok &
	;;
-preferences)
	. $APPDIR/gui_mixer #normalize
	case $2 in
		-playback)		PAGE_NR=1;;
		-lyrics)		PAGE_NR=2;;
		-radiohits)		PAGE_NR=3; PAGE_NR2=1;;
		-video)			PAGE_NR=3; PAGE_NR2=0;;
		*)				PAGE_NR=0;;
	esac
	$APPDIR/func_svg -heading "$(gettext 'Preferences')" > /dev/null
	S='
<window title="pMusic - '$(gettext 'Preferences')'" icon-name="pmusic">
<vbox>
 <vbox '$SF'>
  <hbox spacing="0" height="75" scrollable="true" '$ST' vscrollbar-policy="2" hscrollbar-policy="2">
   <eventbox name="svgheading" '$ST'>
    <hbox '$ST'>
    <text width-request="20" '$SF'><label>""</label></text>
    <text ypad="37" use-markup="true" '$SF'><label>"<i>'$(gettext 'To manually edit any of the weird settings,
press the 'edit' button to the right.')'</i>"</label></text>
    <text '$ST'><label>""</label></text>
     <hbox spacing="0" '$SF'>
      <vbox '$SF'>
       <button relief="2" can-focus="false" tooltip-text="'$(gettext 'Edit rc file')'" '$SF'>
        <input file stock="gtk-edit"></input>
        <action>export GTK2_RC_FILES='$HOME'/.gtkrc-2.0; '$TEXT_EDITOR' $HOME/.pmusic/pmusicrc</action>
       </button>
       <text '$ST'><label>""</label></text>
      </vbox>
      <vbox '$SF'>
       <button relief="2" can-focus="false" '$SF'>
        <input file stock="gtk-dialog-warning"></input>
        <action>'$APPDIR'/func -about -disclaimer > /dev/null &</action>
       </button>
       <text '$ST'><label>""</label></text>
      </vbox>
      <vbox>
       <button relief="2" can-focus="false" '$SF'>
        <input file stock="gtk-help"></input>
        <action>'$0' -info > /dev/null</action>
       </button>
       <text '$ST'><label>""</label></text>
      </vbox>
     </hbox>
    </hbox>
   </eventbox>
  </hbox>
 </vbox>

 <notebook tab-pos="0" page="'$PAGE_NR'" labels="pMusic|'$(gettext 'Playback')'|'$(gettext 'Track info')'|'$(gettext 'Misc')'" '$ST'>

  <vbox>
   <notebook tab-pos="2" labels="'$(gettext 'General')'|'$(gettext 'External progams')'" '$ST'>
    <vbox margin="10">
     <text width-chars="1" height-request="5"><label>""</label></text>
     <hbox>
      <text><label>'$(gettext 'Default file-browser path')'</label></text>
      <entry accept="directory">
       <variable>BROWSERDIR</variable>
       <input>echo $BROWSERDIR</input>
      </entry>
      <button>
       <input file stock="gtk-open"></input>
       <action type="fileselect">BROWSERDIR</action>
      </button>
     </hbox>
     <text><label>""</label></text>
     <hbox>
      <text><label>'$(gettext 'Language')'</label></text>
      <comboboxtext width-request="150">
       <variable>MY_LANGUAGE</variable>
       <item>'$MY_LANGUAGE'</item>
       <item>auto</item>
       '$LANGUAGES'
      </comboboxtext>
      <button width-request="45">
       <label>www</label>
       <action>export GTK2_RC_FILES='$HOME'/.gtkrc-2.0; '$BROWSER' http://www.murga-linux.com/puppy/viewtopic.php?p=259090 &</action>
      </button>
     </hbox>
     <hbox>
      <text><label>'$(gettext 'Theme')'</label></text>
      <comboboxtext width-request="150">
       <variable>THEME</variable>
       <item>'$THEME'</item>
       '$THEMES'
      </comboboxtext>
      <button width-request="45">
       <label>www</label>
       <action>export GTK2_RC_FILES='$HOME'/.gtkrc-2.0; '$BROWSER' http://www.murga-linux.com/puppy/viewtopic.php?t=47639 &</action>
      </button>
     </hbox>
     <hbox>
      <text><label>'$(gettext 'Frontend (window preset)')'</label></text>
      <comboboxtext width-request="150">
       <variable>PMUSIC_FRONTEND</variable>
       <item>'$PMUSIC_FRONTEND'</item>
       '$PMUSIC_FRONTENDS'
      </comboboxtext>
      <button width-request="45">
       <label>www</label>
       <action>export GTK2_RC_FILES='$HOME'/.gtkrc-2.0; '$BROWSER' http://www.murga-linux.com/puppy/viewtopic.php?p=313250#313250 &</action>
      </button>
     </hbox>
     <text><label>""</label></text>
     <checkbox><label>'$(gettext 'Show tooltips')'</label><variable>TOOLTIPS</variable><default>'$TOOLTIPS'</default></checkbox>
     <vbox '$ST'>
      <text><label>""</label></text>
     </vbox>
    </vbox>
   
    <vbox margin="10">
     <hbox>
      <text><label>'$(gettext 'Web browser')'</label></text>
      <entry width-request="200" '$SF'><variable>BROWSER</variable><default>"'$BROWSER'"</default></entry>
     </hbox>
     <hbox>
      <text><label>'$(gettext 'Audio editor')'</label></text>
      <entry width-request="200" '$SF'><variable>AUDIO_EDITOR</variable><default>"'$AUDIO_EDITOR'"</default></entry>
     </hbox>
     <hbox>
      <text><label>'$(gettext 'Text editor')'</label></text>
      <entry width-request="200" '$SF'><variable>TEXT_EDITOR</variable><default>"'$TEXT_EDITOR'"</default></entry>
     </hbox>
     <hbox>
      <text><label>'$(gettext 'Image editor')'</label></text>
      <entry width-request="200" '$SF'><variable>IMAGE_EDITOR</variable><default>"'$IMAGE_EDITOR'"</default></entry>
     </hbox>
    </vbox>
   </notebook>
  </vbox>



  '$TAB_PLAYBACK'



 <vbox>
  <notebook tab-pos="2" labels="'$(gettext 'Audio files')'|'$(gettext 'CD audio')'" '$ST'>
   <vbox margin="10">
     <checkbox>
      <label>'$(gettext 'Extended track info   (Resource hungry for low powered systems)')'</label>
      <variable>USE_META</variable>
      <default>'$USE_META'</default>
     </checkbox>
     <text xalign="1" wrap="false"><label>'$(gettext 'Album art in main window')'</label></text>
     <text xalign="1" wrap="false"><label>'$(gettext 'Update info for playing track')'</label></text>
     <text xalign="1" wrap="false"><label>'$(gettext 'Fetch trackinfo to db for later use')'</label></text>'
     if type streamripper > /dev/null 2>&1; then S=$S'<text xalign="1" wrap="false"><label>'$(gettext 'Name of playing track for radio streams')'</label></text>'; fi
     S=$S'<hseparator height-request="20"></hseparator>
      <text use-markup="true" xalign="0" '$ST'><label>"<b>'$(gettext 'Local storage')'</b> - '$(gettext 'Save track info on harddrive for later use.')'"</label></text>
      <expander>
       <hbox '$ST'>
        <text '$SF'><label>"   "</label></text>
        <text '$SF'><label>"'$(gettext 'pMusic will prefer track-info stored locally over grabbing from internet. This is faster and not depending on connection to the website.

pMusic detects most of the track-info, but depends on meta-tags for at least artist and track-title. Check out the mass-tagger tool in the Music-sources menu.

Album list checks if you got tracks of the matching album, and builds a *.pmu file for later use. This adds new features, but is also the most cpu-consuming option.')'"</label></text>
        <text '$ST'><label>""</label></text>
       </hbox>
       <label>'$(gettext 'More info')'</label>
      </expander>
      <vbox '$ST'>
       <text><label>""</label></text>
      </vbox>
      <vbox '$SF'>
       <checkbox>
        <variable>LYRICS_LOCAL</variable>
        <default>'$LYRICS_LOCAL'</default>
        <label>'$(gettext 'Lyrics')'</label>
       </checkbox>
       <checkbox>
        <variable>ALBUMART_LOCAL</variable>
        <default>'$ALBUMART_LOCAL'</default>
        <label>'$(gettext 'Album artwork')'</label>
       </checkbox>
       <checkbox>
        <variable>PMU_LOCAL</variable>
        <default>'$PMU_LOCAL'</default>
        <label>'$(gettext 'Album list')'</label>
       </checkbox>
      </vbox>

     </vbox>
     <vbox>
      '$TAB_CDDB'
     </vbox>
    </notebook>
   </vbox>

   <vbox>
    <notebook tab-pos="2" page="'$PAGE_NR2'" labels="'$(gettext 'Visualization')'|'$(gettext 'Radio Grabber')'" '$ST'>


     <vbox margin="10">
      <checkbox '$SF'>
       <variable>VIDEO_SERVER</variable>
       <default>'$VIDEO_SERVER'</default>
       <label>'$(gettext 'Activate Visualization engine')'</label>
      </checkbox>
      <text xalign="0" use-markup="true"><label>"'$(gettext 'The engine (video server) will be (de)activated for <b>next</b> track. Be aware that this requires some cpu-usage, so please avoid on low-powered systems.')'"</label></text>
     </vbox>


     <vbox margin="10">
      <checkbox '$SF'>
       <variable>RADIOHITS_DEL_ADDS</variable>
       <default>true</default>
       <label>'$(gettext 'Delete tracks smaller than 1Mb (adverts?)')'</label>
      </checkbox>
     </vbox>

    </notebook>
   </vbox>
  </notebook>

 
  <vbox '$SF'>
   <hbox '$ST'>
    <hbox '$ST' tooltip-text="'$TEXT0'">
     <text use-markup="true" '$SF'><label>"<b><big>'$(gettext 'Data storage')'</big></b>"</label></text>
     <entry accept="directory" fs-title="pMusic - '$(gettext 'Data storage')'" '$ST'>
      <variable>STORAGE_DIR_NEW</variable>
      <default>"'$STORAGE_DIR'"</default>
     </entry>
     <button '$SF'>
      <input file stock="gtk-open"></input>
      <action type="fileselect">STORAGE_DIR_NEW</action>
     </button>
    </hbox>
    <text width-request="50"><label>""</label></text>
    <hbox '$SF'>
     <button cancel></button>
     <button ok></button>
     <statusbar width-request="22"></statusbar>
    </hbox>
   </hbox>
  </vbox>
 </vbox>
</window>'

	[ $TOOLTIPS = false ] && S="`echo "$S" | sed 's%tooltip-%tooltipXXX%g'`" #deactivate tooltips
	echo "$S" | sed 's/##.*//' > $WORKDIR/xml_pmusic_preferences #I use double hash (##) for comments. --> as #FF0000
	I=$IFS; IFS=""
	for STATEMENTS in $($GTKDIALOG -f $WORKDIR/xml_pmusic_preferences); do
		eval $STATEMENTS
	done
	IFS=$I
	if [ "$EXIT" = "OK" ]; then
		if [ "$STORAGE_DIR" != "$STORAGE_DIR_NEW" ]; then #data storage has change. We need to move. 
			if [ ! -d "$STORAGE_DIR_NEW"/history_files ]; then
				mkdir "$STORAGE_DIR_NEW"/history_files
				mkdir "$STORAGE_DIR_NEW"/albumart
				mkdir "$STORAGE_DIR_NEW"/lyrics
				mkdir "$STORAGE_DIR_NEW"/pmu
				mkdir "$STORAGE_DIR_NEW"/favorite_lists
			fi
			mv "$STORAGE_DIR"/albumart/* "$STORAGE_DIR_NEW"/albumart/
			mv "$STORAGE_DIR"/lyrics/* "$STORAGE_DIR_NEW"/lyrics/
			mv "$STORAGE_DIR"/pmu/* "$STORAGE_DIR_NEW"/pmu/
			mv "$STORAGE_DIR"/history_files/* "$STORAGE_DIR_NEW"/history_files/
			#update favorites
			while read I; do
				[ ! "`grep -F "$I" "$STORAGE_DIR_NEW"/favorites`" ] && echo "$I" >> "$STORAGE_DIR_NEW"/favorites
			done <<< $(cut -d'|' -f1 "$STORAGE_DIR"/favorites)
			#update index
			if [[ -s "$STORAGE_DIR"/index_mymusic && ! -s "$STORAGE_DIR_NEW"/index_mymusic ]]; then
				mv "$STORAGE_DIR"/index_* "$STORAGE_DIR_NEW"/
			fi
			#update pmu-path in db
			( flock -e 200
				sed -i "s|$STORAGE_DIR|$STORAGE_DIR_NEW|g" "$STORAGE_DIR_NEW/index_mymusic"
			) 200>$WORKDIR/mymusic_db.lockfile 
		fi
		STORAGE_DIR="$STORAGE_DIR_NEW"
		write_config
		#if USE_META is changed, clear old info
		if [ $USE_META = false ]; then
			$APPDIR/func_svg -nowplaying "pMusic $VERSION" &
			echo "pMusic $VERSION" > $WORKDIR/window_title
			cp -f $APPDIR/themes/$THEME/album_art.svg $WORKDIR/albumart.jpg 2>> $STDERR
			cp -f $APPDIR/themes/$THEME/album_art.svg $HOME/.pmusic/nowplaying_albumart.jpg 2>> $STDERR
		else
			rm $HOME/.pmusic/nowplaying
		fi
		#---
		rm -f $WORKDIR/xml_pmusic* #remove xml-files to force fresh load of all guis (langauge change) 
		pmusic #new start to load changed settings
		exit 0
	fi
	;;

-write_config) 
	write_config
	;;

-version_upgrade) 
	#these must be changed
	export VERSION=`grep -F VERSION $APPDIR/pmusicrc | cut -d= -f2`
	write_config
	;;
-write_frontend_geometry)
	#save geometry for frontend in unique rc file
	FRONTEND="$2"
	[ -s $WORKDIR/window_title ] && XWININFO="`LC_ALL=C xwininfo -stats -name "$(<$WORKDIR/window_title)" 2>> $STDERR`"
	GEOMETRY=${XWININFO#*-geometry }
	GEOMETRY=${GEOMETRY%%\+*}
	WINDOW_WIDTH=${GEOMETRY%x*}
	WINDOW_HEIGHT=${GEOMETRY#*x}
	read X1 C <<< ${XWININFO#*Absolute upper-left X:  }
	read Y1 C <<< ${XWININFO#*Absolute upper-left Y:  }
	read X2 C <<< ${XWININFO#*Relative upper-left X:  }
	read Y2 C <<< ${XWININFO#*Relative upper-left Y:  }
	WINDOW_X=$(($X1-$X2))
	WINDOW_Y=$(($Y1-$Y2))
	#---
	echo "#Geometry for $FRONTEND frontend
export WINDOW_HEIGHT=\"$WINDOW_HEIGHT\"
export WINDOW_WIDTH=\"$WINDOW_WIDTH\"
export WINDOW_X=\"$WINDOW_X\"
export WINDOW_Y=\"$WINDOW_Y\"" > $HOME/.pmusic/pmusicrc"$FRONTEND"

esac
